package com.apps.ftapp.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.OrderDetailModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.DateUtil;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

/**
 * 订单详情
 */
public class MyOrderDetailActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.order_shouhuoren)
    TextView orderShouhuoren;
    @BindView(R.id.order_dizhi)
    TextView orderDizhi;
    @BindView(R.id.order_title)
    TextView orderTitle;
    @BindView(R.id.order_desc)
    TextView orderDesc;
    @BindView(R.id.order_price)
    TextView orderPrice;
    @BindView(R.id.order_img)
    ImageView orderImg;
    @BindView(R.id.order_status)
    TextView orderStatus;
    @BindView(R.id.order_money)
    TextView orderMoney;
    @BindView(R.id.order_wuliu)
    TextView orderWuliu;
    @BindView(R.id.wuliu_no)
    TextView wuliuNo;
    @BindView(R.id.order_no)
    TextView orderNo;
    @BindView(R.id.order_time)
    TextView orderTime;
    @BindView(R.id.order_mid_container)
    RelativeLayout orderMidContainer;
    @BindView(R.id.wuliu_container)
    RelativeLayout wuliuContainer;


    OrderDetailModel result = new OrderDetailModel();
    @BindView(R.id.order_t)
    TextView orderT;

    private Gson gson = new Gson();

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_detail);
        ButterKnife.bind(this);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String id = bundle.getString("id");

        setTitle("订单详情");
        loadData(id);


        orderMidContainer.setOnClickListener(this);


        wuliuContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap map = new HashMap();
                map.put("company", result.getData().getGongsi());
                map.put("no", result.getData().getKuaididanhao());
                ActivitySkipUtil.skipAnotherActivity(MyOrderDetailActivity.this, WuLiuActivity.class, map);
            }
        });
    }

    private void loadData(String id) {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("id", id);
        Async.post("my/order/detail", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));

                    result = gson.fromJson(new String(responseBody), OrderDetailModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {


                        if (null != result.getData().getAddress() && result.getData().getAddress().size() > 0) {
                            orderDizhi.setText("收货人地址：" + result.getData().getAddress().get(0).getAddress());
                            orderShouhuoren.setText("收货人：" + result.getData().getAddress().get(0).getUser_name());
                            orderT.setText(result.getData().getAddress().get(0).getMobile());
                        }


                        orderTitle.setText(result.getData().getTitle());
                        orderDesc.setText("已购" + result.getData().getStudentNum() + "/限购" + result.getData().getMaxStudentNum());
                        orderPrice.setText("¥" + result.getData().getPrice());
                        orderMoney.setText("下单时间：" + DateUtil.timeStamp2Date(result.getData().getCreatedTime(), "yyyy-MM-dd hh:mm:ss"));
                        orderMoney.setVisibility(View.GONE);
                        orderStatus.setText(Tools.getSt(result.getData().getStatus()));

                        orderNo.setText("订单编号：" + result.getData().getSn());
                        orderTime.setText("下单时间：" + DateUtil.timeStamp2Date(result.getData().getCreatedTime(), "yyyy-MM-dd hh:mm:ss"));

                        imageLoader.displayImage(result.getData().getLargePicture(), orderImg, options);


                        orderWuliu.setText(result.getData().getGongsi());
                        wuliuNo.setText(result.getData().getKuaididanhao());

                        if ("0".equals(Tools.isNull(result.getData().getIfaddress()))) {
                            wuliuContainer.setVisibility(View.GONE);
                            orderShouhuoren.setVisibility(View.GONE);
                            orderDizhi.setVisibility(View.GONE);
                        } else {
                            wuliuContainer.setVisibility(View.GONE);
                            orderShouhuoren.setVisibility(View.VISIBLE);
                            orderDizhi.setVisibility(View.VISIBLE);
                        }
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_mid_container:

                //判断是图书 还是 课程
                if ("course".equals(result.getData().getTargetType())) {
                    HashMap<String, Object> map = new HashMap();
                    map.put("id", result.getData().getTargetId());
                    map.put("title", result.getData().getTitle());
                    ActivitySkipUtil.skipAnotherActivity(MyOrderDetailActivity.this, LessonDetailActivity.class, map);
                } else {
                    HashMap<String, Object> params = new HashMap<>();
                    params.put("id", result.getData().getTargetId());
                    ActivitySkipUtil.skipAnotherActivity(MyOrderDetailActivity.this, BookDetailActivity.class, params);
                }


                break;
        }
    }
}
